#include "Reactor.hpp"
#include "Connection.hpp"
#include "Listener.hpp"
#include "Potorol.hpp"
#include "NetCal.hpp"
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port: ..." << std::endl;
        exit(USAGE_ERROR);
    }
    Enable_Console_Log_Strategy();
    uint16_t  port = std::stoi(argv[1]);

    //应用层
    std::shared_ptr<Cal> cal = std::make_shared<Cal>();

    //协议层
    std::shared_ptr<Potorol> pot = std::make_shared<Potorol>([&cal](Request& req)->Response{
        return cal->Excute(req);
    });

    //网络层管理缓冲区
    std::shared_ptr<Connection> Con = std::make_shared<Listener>(port);

    //注册回调方法
    Con->RegisterHandler([&pot](std::string& inbuffer)->std::string{
        std::string resp_str;
        while(true)
        {
            std::string packag;
            if(!pot->Decode(inbuffer,&packag))
                break;

            resp_str += pot->Excute(packag);
        }
        return resp_str;
    });

    //网络层服务器
    std::unique_ptr<Reactor> tscv = std::make_unique<Reactor>();
    
    tscv->AddConnection(Con);

    tscv->Loop();
    return 0;
}